/* Copyright (c) 2004-2023 Peigen.info. All rights reserved. */

package info.peigen.hotpot.manage.core.service.processor.auth.check;

import cn.dev33.satoken.stp.StpUtil;
import info.peigen.hotpot.common.core.exception.SystemException;
import info.peigen.hotpot.common.service.base.processor.Processor;
import info.peigen.hotpot.common.service.base.processor.ProcessorServiceContext;
import info.peigen.hotpot.manage.core.service.order.LoginOrder;
import info.peigen.hotpot.manage.core.service.processor.AbstractHotpotManageServiceProcessor;
import org.noear.solon.annotation.Component;

/**
 * <b>(AuthLoginCheckProcessor)</b>
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2023/8/30
 */
@Component
public class AuthLoginCheckProcessor extends AbstractHotpotManageServiceProcessor implements Processor {
    @Override
    public Integer level() {
        return 21;
    }

    @Override
    public void execute(ProcessorServiceContext serviceContext) {
        String loginName = getLoginName(serviceContext);

        // disableTime = -2表示未被封禁
        long disableTime = StpUtil.getDisableTime(loginName);
        if (disableTime > 0) {
            if (disableTime > 60) {
                throw new SystemException(loginName + "账号已被封禁, 请再" + disableTime / 60 + "分钟后重新尝试登录!!");
            }
            throw new SystemException(loginName + "账号已被封禁, 请再" + disableTime + "秒后重新尝试登录!!");
        }
    }

    private String getLoginName(ProcessorServiceContext context) {
        if (context.getOrder() instanceof LoginOrder order) {
            return order.getLoginName();
        }
        return null;
    }
}
